package com.ygu.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ygu.ecare.R;
import com.ygu.ecare.utils.HTTPGetTool;
import com.ygu.ecare.utils.URLUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ChooseYY extends Activity {
	private ProgressDialog prodialog = null;
	private List<Map<String, Object>> yylist = null;
	private ListView yylistview = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.chooseyy);
		yylistview = (ListView) findViewById(R.id.hospital);
		getHospitalInfo();
		setOnClickListener();
		
	}
	private void setOnClickListener(){
		yylistview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent it = getIntent();
				TextView yymc = (TextView) view.findViewById(R.id.yymc);
				TextView yydm = (TextView) view.findViewById(R.id.yydm);
				it.putExtra("yymc", yymc.getText().toString());
				it.putExtra("yydm", yydm.getText().toString());
				ChooseYY.this.setResult(2, it);
				ChooseYY.this.finish();
			}
			
		});
	}
	private void getHospitalInfo(){
		prodialog = new ProgressDialog(this);
		prodialog.setMessage("加载中");
		prodialog.show();
		new Thread(new loadHospitalInfo()).start();
	}
	class loadHospitalInfo implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			Message msg = myHandler.obtainMessage();
			Intent it = getIntent();
			List<Map<String,Object>> yylisttemp = new ArrayList<Map<String,Object>>();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("orgid", it.getExtras().getString("dqid")));
			JSONObject hospital = HTTPGetTool.getTool().post(URLUtils.HOSTMAIN+URLUtils.GETYYLIST, params);

			JSONArray yyarray = null;
			try {
				yyarray = hospital.getJSONArray("yyinfo");

			for(int i=0;i<yyarray.length();i++){
				Map<String,Object> map = new HashMap<String,Object>();
				JSONObject json = yyarray.getJSONObject(i);
				map.put("yydm", json.getString("yydm"));
				map.put("yymc", json.getString("yymc"));
				yylisttemp.add(map);
			}
			 msg.what=1;
			 msg.obj=yylisttemp;
			 myHandler.sendMessage(msg);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	Handler myHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch(msg.what){
			case 1:
				prodialog.dismiss();
				yylist = (List<Map<String, Object>>) msg.obj;
				SimpleAdapter myAdapter = new SimpleAdapter(ChooseYY.this, yylist, R.layout.data_yy, new String[]{"yydm","yymc"}, new int[]{
						R.id.yydm,R.id.yymc});
				yylistview.setAdapter(myAdapter);
			}

		}
	};

}
